package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.expression.facade.ExpressionFacade;

public class TextDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		str.append("<input type=\"text\" id=\"").append(formColumnDomain.getName()).append("\" name=\"")
			.append(formColumnDomain.getName());
		String className = "input_short";
		str.append("\" class=\"").append(className).append("\" maxlength=\"120\"").append(" value=\"")
			.append(MapUtil.getStringValue(formMap, formColumnDomain.getName())).append("\"/>");
				
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		StringBuffer str = new StringBuffer();
		str.append("<input type=\"text\" id=\"").append(formColumnDomain.getName()).append("\" name=\"")
			.append(formColumnDomain.getName());
		String className = "input_middle";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			className = formColumnDomain.getShowclass();
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		if(name.indexOf("~") > 0)
			name = name.substring(name.indexOf("~") + 1);
		String value = MapUtil.getStringValue(formMap, name);
		if(StringUtil.isEmpty(value))
			value = ExpressionFacade.getExpressionValue(formMap, formColumnDomain.getExpression());
		if(StringUtil.isEmpty(value))
			value = formColumnDomain.getDefaultvalue();
		str.append("\" class=\"").append(className).append("\" maxlength=\"120\"").append(" value=\"")
			.append(value).append("\" ");
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			str.append(formColumnDomain.getJs());
		if(!StringUtil.isEmpty(formColumnDomain.getShowcontrol()))
			str.append(formColumnDomain.getShowcontrol());
		str.append("/>");
		if(formColumnDomain.isIsrequired())
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		if(!StringUtil.isEmpty(formColumnDomain.getNotemsg()))
			str.append(formColumnDomain.getNotemsg());
		if(!StringUtil.isEmpty(formColumnDomain.getPopfullup()))
			str.append(formColumnDomain.getPopfullup());
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		if(name.indexOf("~") > 0)
			name = name.substring(name.indexOf("~") + 1);
		return MapUtil.getStringValue(formMap, name, "&nbsp;");
	}
	
	public void dealFormValue(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(!StringUtil.isEmpty(formColumnDomain.getCountexpression()))
			ExpressionFacade.getFormValue(formMap, formColumnDomain.getName(), formColumnDomain.getCountexpression());
	}
}